
#pragma once

#include <ConnectionBroker/clientconnection/ClientConnection.h>
#include <ConnectionBroker/clientconnection/HttpReply.h>
#include <ConnectionBroker/clientconnection/HttpRequestParser.h>
#include <ConnectionBroker/clientconnection/HttpRequest.h>

#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/noncopyable.hpp>

namespace ConnectionBroker
{
   class LongPollServer;
   class LongPollConnection : public boost::enable_shared_from_this<LongPollConnection>,
                              private boost::noncopyable
   {
      protected: //Member variables
      boost::array<char, 8192> mBuffer;
      boost::asio::io_service& mIoService;
      boost::asio::ip::tcp::socket mSocket;
      ConnectionBroker::HttpReply mReply;
      ConnectionBroker::HttpRequest mRequest;
      ConnectionBroker::HttpRequestParser mRequestParser;
      ConnectionBroker::LongPollServer* mServer;
      boost::asio::io_service::strand mStrand;

      public: //Constructor, destructor
      explicit LongPollConnection(boost::asio::io_service& pIoService, ConnectionBroker::LongPollServer* pServer);
      virtual ~LongPollConnection();

      public: //Interface
      void         start();

      public: //Getters
      boost::asio::ip::tcp::socket& socket() { return mSocket; }

      protected: //Utilities
      void fillReply(const std::string& msgStr, ConnectionBroker::HttpReply& rep);
      void handleRead(const boost::system::error_code& pError, std::size_t pBytesTransferred);
      bool handleRequest(const ConnectionBroker::HttpRequest& req, ConnectionBroker::HttpReply& rep);
      void handleWrite(const boost::system::error_code& e);
   }; 
}

